#
# -*- coding: utf8 -*-

from plurk_user import PlurkUser
import jsonizer

class PlurkResponse:
  '''A response to a plurk. Attributes: lang, content_raw, user_id, qualifier,
  plurk_id, content, id, posted.'''
  def __init__(self, pdict):
    self.__dict__.update(pdict)
    if self.__dict__.has_key('posted') and type(self.posted) == str:
      self.posted = jsonizer.datetime_from_plurk_string(self.posted)
  def __str__(self):
    result = u'%s%s %s' % (self.user_id, 
        ' '+self.qualifier+':' if self.qualifier != ':' else ':', 
        self.content_raw)
    return result.encode('utf8')

class PlurkResponseList:
  '''A list of responses to a plurk and their posters.'''
  def __init__(self, pdict):
    '''Construct a PlurkResponse from a dictionary.'''
    self.datadict = pdict
    self.num_responses = pdict['responses_seen']
    self.response_list = [PlurkResponse(dic) for dic in pdict['responses']]
    fdict = pdict['friends']   # dict of friends who posted responses
    self.poster_dict = dict((int(k),PlurkUser(v)) for k,v in fdict.iteritems())

  def __iter__(self):
    return self.response_list.__iter__()

  def __str__(self):
    result = [u'responses [']
    result.append(u', '.join([u'%s%s %s' % (
        self.poster_dict[r.user_id].username,
        r.qualifier if r.qualifier == ':' else ' '+r.qualifier+':',
        r.content_raw) for r in self.response_list]))
    result.append(u']')
    return u''.join(result).encode('utf8')

  def __eq__(self, other):
    return self.__class__ == other.__class__ and \
           self.num_responses == other.num_responses and \
           self.response_list == other.response_list and \
           self.poster_dict == other.poster_dict
